import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path:"/login",
    component:() => import(/*webpackChunkName:"laogin"*/'../views/login')
  },
  {
    path: '/home',
    component: ()=>import('@/views/home/index.vue'),
    children:[
      {
        path:"homes",
        component:()=>import('@/views/home/homes'),
      },
      {
        path:"findhouse",
        component:()=>import('@/views/home/findhouse'),
        meta:{
          keepAlive:true,
        }
      },
      {
        path:"my",
        component:()=>import('@/views/home/my'),
      },
      {
        path:"/home",
        redirect:"homes"
      }
    ]
  },
  {
    path: '/cityList',
    name: 'cityList',
    component: () => import(/* webpackChunkName: "city" */ '../views/cityList/cityList.vue')
  },
  {
    path:"/rent",
    component:() => import('@/views/rent'),
    children:[
      {
        path:"/rent/add",
        component: () => import('@/views/rent/add'),
      },
      {
        path:"/rent/search",
        component: () => import('@/views/rent/search'),
      },
      {
        path:"/rent",
        redirect:"/rent/add"
      }
    ]
  },
  {
    path: '/map',
    component: () => import('@/components/map.vue')
  },
  {
    path: '/detail/:id',
    component: () => import('@/views/houseDetail')
  },
  {
    path: '/vr',
    component: () => import('@/views/vr')
  },
  {
    path:"/",
    redirect:"/home"
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


router.beforeEach((to,from,next)=>{
  const token = sessionStorage.getItem('token');
  console.log(to);
  if(to.path .includes('/rent') && !token){
    next('/login');
  } 
  console.log(token);
  next()
 
})



export default router
